import fmeobjects
import numpy as np


class FeatureCreator(object):
    """Template Class Interface:
    When using this class, make sure its name is set as the value of the 'Class
    to Process Features' transformer parameter.
    """

    def __init__(self):
        """Base constructor for class members."""
        pass

    def input(self, feature):
        """This method is called once by FME to create FME Feature(s).
        A created Feature is emitted through the self.pyoutput() method.
        Any number of Features can be created and emitted by this method.

        :param fmeobjects.FMEFeature feature: Dummy FME Feature passed in by
            FME. It can be ignored.
        """
        newFeature = fmeobjects.FMEFeature()
        mesh = fmeobjects.FMEMesh()
        num_segments_per_circle = 36
        pipe_height = 0.2
        join_length = 0.3
        ptO = np.asarray([-0.9, 5.2, 0.6])
        ptA = np.asarray([-2.47, 1.87, 0.5])
        ptB = np.asarray([3.1, 6.5, 0.6])
        ptC = np.asarray([-1.1, 5.2, 5])
        ptD = np.asarray([-4.3, 9.3, 0])
        ptE = np.asarray([-7.6, 3.8, 3])
        ptF = np.asarray([3.4, -0.9, -3.7])
        vertices_array = calculate_nt(num_segments_per_circle, pipe_height * 0.5, join_length, ptO, ptA, ptB, ptC, ptD,
                                      ptE, ptF)
        vertices_array_count = len(vertices_array)
        vertices = np.concatenate(vertices_array)

        for vertex in vertices:
            mesh.appendVertex(vertex[0], vertex[1], vertex[2])

        num_segments_per_circle = num_segments_per_circle + 1
        for m in range(num_segments_per_circle):
            if m < num_segments_per_circle - 1:
                for n in range(num_segments_per_circle):
                    if n == 0:
                        mesh.addMeshPart(0, [n + m * num_segments_per_circle, n + m * num_segments_per_circle + 1,
                                             n + m * num_segments_per_circle + num_segments_per_circle + 1],
                                         None,
                                         None)

                    elif (n > 0) and (n < num_segments_per_circle - 2):
                        mesh.addMeshPart(0, [n + m * num_segments_per_circle, n + m * num_segments_per_circle + 1,
                                             n + m * num_segments_per_circle + num_segments_per_circle + 1,
                                             n + m * num_segments_per_circle + num_segments_per_circle],
                                         None,
                                         None)
                    elif n == num_segments_per_circle - 2:
                        mesh.addMeshPart(0, [n + m * num_segments_per_circle,
                                             n + m * num_segments_per_circle + 1,
                                             n + m * num_segments_per_circle + num_segments_per_circle],
                                         None,
                                         None)
        print(vertices_array_count)
        for i in range(1, vertices_array_count, 2):
            # vertices_start = vertices[i]
            # vertices_end = vertices[i + 1]
            tmp_index_array = []
            for k in range(num_segments_per_circle):
                tmp_index_array.append(
                    num_segments_per_circle * num_segments_per_circle + (i - 1) * num_segments_per_circle + k)
            print(tmp_index_array)
            mesh.addMeshPart(0, tmp_index_array, None, None)
            for j in range(num_segments_per_circle):
                mesh.addMeshPart(0, [
                    num_segments_per_circle * num_segments_per_circle + (i - 1) * num_segments_per_circle + j + 1,
                    num_segments_per_circle * num_segments_per_circle + (i - 1) * num_segments_per_circle + j,
                    num_segments_per_circle * num_segments_per_circle + i * num_segments_per_circle + j,
                    num_segments_per_circle * num_segments_per_circle + i * num_segments_per_circle + j + 1],
                                 None,
                                 None)
        newFeature.setGeometry(mesh)
        self.pyoutput(newFeature)

    def close(self):
        """This method is called at the end of the class."""
        pass
